/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.Giuong_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class Giuong_DAO {
    public static int Get_MaGI() throws SQLException{
        int MaGI = 0;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call ADD_ID(?)}");
        statement.setString(1, "GI");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaGI = Rs.getInt(1);
        }
        return MaGI;
    }
    
    public static ArrayList<Giuong_DTO> GetALL()
    {
        ArrayList<Giuong_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from Giuong" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                Giuong_DTO Giuong = new Giuong_DTO();
                Giuong.setMaGiuong(rs.getInt("MaGiuong"));
                Giuong.setTang(rs.getInt("Tang"));
                Giuong.setTrangThai(rs.getInt("TrangThai"));
                Giuong.setMaPhong(rs.getString("MaPhong"));
                ds.add(Giuong);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(Giuong_DTO Giuong)
    {
        boolean flag = false;
        try {
            String sql = "Insert Giuong Values('"+Giuong.getMaGiuong()+"','"+Giuong.getTang()+"','"+Giuong.getTrangThai()+"','"+ Giuong.getMaPhong() +"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(Giuong_DTO Giuong)
    {
        boolean flag = false;
        try {
            String sql = "Update Giuong Set Tang ='"+ Giuong.getTang() +"', TrangThai = "+ Giuong.getTrangThai() +", MaPhong = '"+Giuong.getMaPhong()+"' where MaGiuong = "+ Giuong.getMaGiuong();
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(int maGiuong)
    {
        boolean flag = false;
        try {
            String sql = "Delete from Giuong where MaGiuong = "+ maGiuong;
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
}
